﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Server
{
    public class CLog
    {
        #region "Biến"
        private static string _pathlog = "./Log/log.ini";

        public static string Pathlog
        {
            get { return _pathlog; }
            set { _pathlog = value; }
        }

        static StreamReader _reader;

        static StreamWriter _writer;

        #endregion

        #region "Hàm"
        /// <summary>
        /// Ghi log
        /// </summary>
        /// <param name="log">Nhật ký gửi</param>
        public static void GhiLog(string log)
        {
            FileStream stream = File.Open(_pathlog, FileMode.Append);
            _writer = new StreamWriter(stream);
            log = "</s/>\n" + log + "\n</e/>";
            _writer.WriteLine(log);
            _writer.Flush();

            _writer.Close();
        }

        /// <summary>
        /// Đọc nhật ký
        /// </summary>
        /// <returns></returns>
        public static List<string> DocLog()
        {
            List<string> liststr = new List<string>();

            _reader = new StreamReader(File.Open(_pathlog, FileMode.OpenOrCreate));
            while (!_reader.EndOfStream)
            {
                string start = _reader.ReadLine();
                string log = string.Empty;
                if (start.Equals("</s/>"))
                {
                    while (!_reader.EndOfStream)
                    {
                        string end = _reader.ReadLine();
                        if (end.Equals("</e/>"))
                        {
                            break;
                        }
                        log += end;
                    }
                    liststr.Add(log);
                }
            }
            _reader.Close();
            return liststr;

        }
        /// <summary>
        /// Đọc nhật ký
        /// </summary>
        /// <returns></returns>
        public static string DocLogAll()
        {
           // List<string> liststr = new List<string>();

            _reader = new StreamReader(File.Open(_pathlog, FileMode.OpenOrCreate));
            string str = _reader.ReadToEnd();
            _reader.Close();
            return str;

            //while (!_reader.EndOfStream)
            //{
            //    string start = _reader.ReadLine();
            //    string log = string.Empty;
            //    if (start.Equals("</s/>"))
            //    {
            //        while (!_reader.EndOfStream)
            //        {
            //            string end = _reader.ReadLine();
            //            if (end.Equals("</e/>"))
            //            {
            //                break;
            //            }
            //            log += end;
            //        }
            //        liststr.Add(log);
            //    }
            //}
            //return liststr;
        }
        #endregion
    }
}
